package me.a02mytreemap;

import java.util.TreeMap;
import java.util.function.BiConsumer;

public class A03_TreeMapDemo3 {
    public static void main(String[] args) {
        //统计字符串中每个字符出现的次数
        String s = "aababcabcdabcde";

        TreeMap<Character, Integer> tm = new TreeMap<>();

        //遍历字符串得到每一个字符
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (tm.containsKey(c)) {
                int count = tm.get(c);
                count++;
                tm.put(c, count);
            } else {
                tm.put(c, 1);
            }
        }
        //遍历集合，按照指定的格式进行拼接
        StringBuilder sb = new StringBuilder();
        tm.forEach(new BiConsumer<Character, Integer>() {
            @Override
            public void accept(Character key, Integer value) {
                sb.append(key).append("(").append(value).append(")");
            }
        });
        System.out.println(sb);
    }
}
